﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using virtuaPol.Main;
using virtuaPol.Model;


namespace vpWorldFileConverter
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                string exe = Path.GetFileName(
                    System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
                Console.WriteLine(String.Format("Usage: {0} sourcefile destination", exe));
                Environment.ExitCode = 1;
                return;
            }
            if(!File.Exists(args[0]))
            {
                Console.WriteLine("The sourcefile you specified doesn't exist!");
                Environment.ExitCode = 1;
                return;
            }

            // Load World object from sourcefile
            CWorld world;
            string extension = Path.GetExtension(args[0]);
            if (!WorldLoader.load(out world, args[0], extension.Equals(".vpw")))
            {
                Console.WriteLine("The sourcefile you specified is invalid!");
                Environment.ExitCode = 1;
                return;
            }

            extension = Path.GetExtension(args[1]);
            if (!WorldLoader.save(ref world, args[1], extension.Equals(".vpw")))
            {
                Console.WriteLine("The world object couldn't be saved to the destination file.");
                Environment.ExitCode = 1;
                return;
            }

            Console.WriteLine("The sourcefile was converted successfully!");
            Environment.ExitCode = 0;
        }
    }
}
